<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

Loader::loadClass('Upload');

class Upload_Image extends Upload
{
    public $exts = array('gif', 'png', 'jpg', 'jpeg', 'bmp');
    public $maxsize = 1024000;
    public $autocat = false;
    public $attachmentdir = 'attachment/img/content/';
    
    public $minwidth = null;
    public $minheight = null;
    public $maxwidth = null;
    public $maxheight = null;
    
    const NOT_IMAGE_FILE = -8;
    const WIDTH_TOO_BIG = -9;
    const HEIGHT_TOO_BIG = -10;
    const WIDTH_TOO_SMALL = -11;
    const HEIGHT_TOO_SMALL = -12;
    
    public function __construct() {}
    
    public function upload() {
        if(!empty($this->_files)) {
            foreach($this->_files as $k => $v) {
                $error = '';

                $imageinfo = getimagesize($v['tmp_name']);
                if(empty($imageinfo)) {
                    $error = self::NOT_IMAGE_FILE;
                } else {
                    list($width, $height, $type) = $imageinfo;
                    if(!in_array($type, array(1,2,3,6,13))) {
                        $error = self::EXT_DENY;
                    } elseif($this->maxwidth && $width > $this->maxwidth) {
                        $error = self::WIDTH_TOO_BIG;
                    } elseif($this->maxheight && $height > $this->maxheight) {
                        $error = self::HEIGHT_TOO_BIG;
                    } elseif($this->minwidth && $width < $this->minwidth) {
                        $error = self::WIDTH_TOO_SMALL;
                    } elseif($this->minheight && $height < $this->minheight) {
                        $error = self::HEIGHT_TOO_SMALL;
                    }
                }

                if('' != $error) {
                    $this->_failed[] = array('filename' => $v['name'], 'error' => $error);
                    $this->_files[$k] = null;
                    unset($this->_files[$k]);
                }
            }
        }
        
        return parent::upload();
    }
}